﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Study
{
    public partial class ProductList : System.Web.UI.Page
    {
        static string dbinfo = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
 

        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += new EventHandler(Button1_Click);   
            if (!Page.IsPostBack)
            {
                settingOptions();
            }
        }

        void Button1_Click(object sender, EventArgs e)
        {
            gvList();
        }

        void settingOptions()
        {
            SqlConnection conn = new SqlConnection(dbinfo);
            SqlCommand cmd_Select = new SqlCommand();
            cmd_Select.Connection = conn;
            cmd_Select.CommandType = CommandType.StoredProcedure;
            cmd_Select.CommandText = "SP_ddlOption_Select";

            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd_Select;

            DataSet ds = new DataSet();
            sda.Fill(ds);

            DataTable dtReseller = new DataTable();
            DataTable dtColor = new DataTable();
            DataTable dtSize = new DataTable();

            dtReseller = ds.Tables[0];
            dtColor = ds.Tables[1];
            dtSize = ds.Tables[2];


            ddlReseller.DataSource = dtReseller;
            ddlReseller.DataValueField = "ResellerKey";
            ddlReseller.DataTextField = "ResellerName";
            ddlReseller.DataBind();

            foreach (DataRow dr in dtColor.Rows)
            {
                ddlColor.Items.Add(dr["Color"].ToString());
            }

            foreach (DataRow dr in dtSize.Rows)
            {
                ddlSize.Items.Add(dr["Size"].ToString());
            }

        }

        void gvList()
        {
            string val1 = ddlReseller.SelectedItem.Value;
            string val2 = ddlColor.SelectedItem.Value;
            string val3 = ddlSize.SelectedItem.Value;

            SqlConnection conn = new SqlConnection(dbinfo);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SP_ProductList_Select";

            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@ResellerKey";
            param1.Value = val1;

            SqlParameter param2 = new SqlParameter();
            param2.ParameterName = "@Color";
            param2.Value = val2;

            SqlParameter param3 = new SqlParameter();
            param3.ParameterName = "@Size";
            param3.Value = val3;

            SqlParameter[] _params = { param1, param2, param3 };
            cmd.Parameters.AddRange(_params);

            conn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            GridView1.DataSource = sdr;
            GridView1.DataBind();
            sdr.Close();
            conn.Close();
        }
    }
}